package com.iplant.mes.controller.sfc;

import com.iplant.base.po.ServiceResult;
import com.iplant.base.utils.RetCode;
import com.iplant.base.utils.serializer.CloneTool;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.basic.controller.BasicController;
import com.iplant.basic.service.po.bms.BMSEmployee;
import com.iplant.mes.service.SFCService;
import com.iplant.mes.service.po.sfc.SFCBoxReport;
import com.iplant.mes.service.po.sfc.SFCWorkpieceRecord;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/SFCBoxReport")
public class SFCBoxReportController extends BasicController<SFCBoxReport> {
    private static final Logger logger = LoggerFactory.getLogger(SFCBoxReportController.class);

    public SFCBoxReportController() {
        super(SFCBoxReport.class);
    }

    @Autowired
    SFCService mSFCService;

    /**
     * 报工
     */
    @PostMapping("/Report")
    public Object Report(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {
            BMSEmployee wLoginUser = GetSession(request);

            List<SFCWorkpieceRecord> wSFCWorkpieceRecordList = CloneTool.CloneArray(wParam.get("data"),
                    SFCWorkpieceRecord.class);

            if (wSFCWorkpieceRecordList == null || wSFCWorkpieceRecordList.size() == 0) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<Integer> wServiceResult = mSFCService.SFC_BoxReport(wLoginUser, wSFCWorkpieceRecordList);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    /**
     * 查单条
     */
    @GetMapping("/Info")
    public Object Info(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            int wBoxReportID = StringUtils.parseInt(request.getParameter("BoxReportID"));

            if (wBoxReportID <= 0) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<SFCBoxReport> wServiceResult = mSFCService.SFC_QueryBoxReportInfo(wLoginUser, wBoxReportID);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }
}
